#!/usr/bin/python

import httplib, hashlib, re, random, sys
import commands, os
import time
import urllib2

#def checkLogin(httpServ):
#  httpServ.request('POST', '/cgi-bin/do_login', 'action=check_online')
#  response = httpServ.getresponse()
#  return response.read()
#
#def logout(httpServ):
#  httpServ.request('POST', '/cgi-bin/do_logout', '')
#  # return : {'logout_ok', 'not_online_error', ...}
#  response = httpServ.getresponse()
#  time.sleep(5)
#  return response.read()
#
#def login(httpServ, ith):
#  httpServ.request('POST', '/cgi-bin/do_login',
#      'username=' + user[ith][1] + '&password=' + user[ith][0] + '&drop=0&type=1&n=100')
#  response = httpServ.getresponse()
#  return response.read()
#
#def do_login():
#  httpServ = httplib.HTTPConnection("", 80)
#  httpServ.connect()
#  # check login
#  res = checkLogin(httpServ)
#  if re.match('^\d+,[^,]+,\d+,\d+,\d+$', res):
#    printLog('1.11 already login: ' + res)
#    # logout
#    res = logout(httpServ)
#    printLog('1.12 logout: ' + res)
#  else: 
#    printLog('1.2 not login: ' + res)
#  # login
#  ith = getNextUserID()
#  printLog('2 choose user: ' + user[ith][1])
#  res = login(httpServ, ith)
#  printLog('3 login: ' + res)
#  # check login
#  res = checkLogin(httpServ)
#  printLog('4 check login: ' + res)
#  # update pool
#  arr = res.split(',')
#  if len(arr) == 5: user[ith][3] = long(arr[2])
#  updatePoolDB()
#  httpServ.close()
#
##

## to some looking glass
#httpServ = httplib.HTTPConnection("www.pitentel.cl", 80)
#httpServ.connect()
#
#httpServ.request('POST', '/cgi-bin/lg.pl',
#    'query=ping&addr=166.111.68.233&router=PIT-IP')
#response = httpServ.getresponse()
#res = response.read()
#
#print "RES: ", res
#httpServ.close()

# to some looking glass
#httpServ = httplib.HTTPConnection("lg.he.net", 80)
#httpServ.connect()
#headers = {"Content-Type":"application/x-www-form-urlencoded",}
#httpServ.request('POST', '/',
#    'routers[]=core1.ber1.he.net&command=ping&ip=166.111.68.233&raw=1',
#    headers)
#response = httpServ.getresponse()
#res = response.read()
#httpServ.close()

lgs = ['core1.ams1.he.net',
    'core1.ber1.he.net',
    'core1.fra1.he.net',
    'core1.lon1.he.net',
    'core1.par1.he.net',
    'core1.par2.he.net',
    'core1.prg1.he.net',
    'core1.sto1.he.net',
    'core1.waw1.he.net',
    'core1.zrh1.he.net',
    'core1.ash1.he.net',
    'core1.atl1.he.net',
    'core1.chi1.he.net',
    'core1.dal1.he.net',
    'core1.den1.he.net',
    'core1.fmt1.he.net',
    'core1.fmt2.he.net',
    'core1.lax1.he.net',
    'core1.lax2.he.net',
    'core1.mci1.he.net',
    'core1.mci2.he.net',
    'core1.mci3.he.net',
    'core1.mia1.he.net',
    'core1.msp1.he.net',
    'core1.nyc1.he.net',
    'core1.nyc4.he.net',
    'core1.nyc5.he.net',
    'core1.pao1.he.net',
    'core1.pdx1.he.net',
    'core1.phx1.he.net',
    'core1.sea1.he.net',
    'core1.sjc1.he.net',
    'core1.sjc2.he.net',
    'core1.tor1.he.net',
    'core1.yvr1.he.net',
    'core1.hkg1.he.net',
    'core1.sin1.he.net',
    'core1.tyo1.he.net',
  ]

ping_req = urllib2.Request(url='http://lg.he.net/',
      data='routers[]=%s&command=ping&ip=%s&raw=1' %
      ('core1.ber1.he.net', '2404:6800:8005::63'))#'166.111.68.233'))
for i in xrange(2):
  beg = int(time.time())
  conn = urllib2.urlopen(ping_req)
  res = conn.read()
  res = re.match('.+lg_return.+<pre>(.+)</pre>', res, re.DOTALL)
  if res:
    res = res.group(1).split('\n')#[1:-1]
    print beg, int(time.time()) - beg
    print '\n'.join(res)

bgp_req = urllib2.Request(url='http://lg.he.net/',
      data='routers[]=%s&command=bgproute&ip=%s&raw=1' %
      ('core1.ber1.he.net', '2404:6800::/32'))#'166.111.0.0/16'))
for i in xrange(2):
  beg = int(time.time())
  conn = urllib2.urlopen(bgp_req)
  res = conn.read()
  res = re.match('.+lg_return.+<pre>.+Status: B.+AS_PATH: ([^\n]+)\n.+</pre>', res, re.DOTALL)
  if res:
    res = res.group(1).split('\n')#[1:-1]
    print beg, int(time.time()) - beg
    print '\n'.join(res)

# resource
# http://lg.retn.net/
